﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Wait.cs" company="SMART business">
//   SMART business.
// </copyright>
// <summary>
//   Defines the Wait type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ComparisonTool.Forms
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    /// <summary>
    /// The wait form.
    /// </summary>
    public partial class Wait : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Wait"/> class.
        /// </summary>
        public Wait()
        {
            this.InitializeComponent();

            this.Paint += this.WaitPaint;

            this.FormBorderStyle = FormBorderStyle.None;
            this.AllowTransparency = true;
            this.BackColor = Color.AliceBlue;
            this.TransparencyKey = this.BackColor;
            this.Opacity = .30;

            this.GotFocus += this.WaitGotFocus;
        }

        private void WaitGotFocus(object sender, EventArgs e)
        {
            if (this.Owner != null)
            {
                this.Owner.Focus();
            }
        }

        private void WaitPaint(object sender, PaintEventArgs e)
        {
            var hatchBrush = new HatchBrush(HatchStyle.Percent50, this.TransparencyKey);
            e.Graphics.FillRectangle(hatchBrush, this.DisplayRectangle);
        }

        private void ParentMove(object sender, EventArgs e)
        {
            if (this.Owner != null)
            {
                this.Top = this.Owner.Top + 30;
                this.Left = this.Owner.Left + 7;
                this.Width = this.Owner.Width - 14;
                this.Height = this.Owner.Height - 37;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether opened.
        /// </summary>
        public bool Opened { get; set; }

        /// <summary>
        /// The wait form_ form closing.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WaitFormFormClosing(object sender, FormClosingEventArgs e)
        {
            this.Opened = false;
        }

        /// <summary>
        /// The wait form_ enter.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WaitFormEnter(object sender, EventArgs e)
        {
            this.Opened = true;
        }

        /// <summary>
        /// The wait form shown.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WaitFormShown(object sender, EventArgs e)
        {
            this.Opened = true;

            this.BringToFront();

            if (this.Owner != null)
            {
                this.Top = this.Owner.Top + 30;
                this.Left = this.Owner.Left + 7;
                this.Width = this.Owner.Width - 14;
                this.Height = this.Owner.Height - 37;

                this.Owner.LocationChanged += this.ParentMove;
                this.Owner.Resize += this.ParentMove;
            }
        }
    }
}
